//https://leetcode.cn/problems/fruit-into-baskets/
//lc904_水果成蓝
class Solution {
public:
    int totalFruit(vector<int>& fruits) {
        unordered_map<int, int> unmap; int left = 0, ans = 0;
        for(int rigth = 0; rigth < fruits.size(); rigth++){
            ++unmap[fruits[rigth]];
            while(unmap.size()> 2){
                auto it = unmap.find(fruits[left]);
                --it->second;
                if(it->second == 0){ unmap.erase(it);}
                left++;
            }
            ans = max(ans , rigth - left + 1);
        }
        return ans;
    }
};